package com.example.learning3;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Calendar;
import java.util.TimeZone;

import android.annotation.SuppressLint;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.Switch;
import android.widget.TextView;
import android.os.Process;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    ///////
    int flag_add_high=0;
    int flag_reduce_high=0;
    int flag_add_anger=0;
    int flag_reduce_anger=0;
    int flag_light_status=0;
    //////////////////////定义变量
    Button button_learning;
    Button button_light;
    Button button_temperature;
    Button button_heat_radiation;
    Button button_add_height;
    Button button_reduce_height;
    Button stop_in_height;
    Button button_add_anger;
    Button button_reduce_anger;
    Button stop_in_anger;
    Button button_analysis;
    Button button_function;
    Button button_self;
    Button button_setting;
    Calendar cal;
    String week;
    String year;
    String month;
    String day;
    String hour;
    String minute;
    String second;
    String my_time_1;
    String my_time_2;

    int startRunTime=0;
    int start_hour=0;
    int start_minute=0;
    int start_second=0;
    int already_learning_time_minute=0;
    int already_learning_time_s=0;
    int already_learning_time_hours=0;
    int learning_time_hour =0;
    int learning_time_minute =0;
    int learning_time_second =0;
    View PopupView_learning;
    TextView textView_already_learning_time;

    int connect_ok=0;
    int status=0;
    Button out_desktop;
    Thread connect_service;
    Button connect_pi;
    TextView connect_status;
    TextView receive_status;
    Socket s;
    String ip="192.168.12.1";
    int port;
    StringBuffer tx;
    StringBuffer rx;
    EditText port_in;

    int save_id=1;
    int int_water_temperature=0;

    private DevicePolicyManager policyManager;
    private ComponentName componentName;      //锁屏
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //////////////////////////界面切换////////////////////
        button_analysis=findViewById(R.id.button_analysis);
        button_function=findViewById(R.id.button_function);
        button_self=findViewById(R.id.button_self);
        button_setting=findViewById(R.id.button_setting);
        ///////////////////////获取变量//////////////////////////////////////////////////////
        button_learning=findViewById(R.id.button_learning);
        button_light=findViewById(R.id.button_light);
        button_temperature=findViewById(R.id.button_temperature);
        button_heat_radiation=findViewById(R.id.button_heat_radiation);
        /////////高度//////////////////////////////////////////////////////////////
        button_add_height=findViewById(R.id.add_height);
        button_reduce_height=findViewById(R.id.reduce_height);
        stop_in_height=findViewById(R.id.stop_in_height);
        ////////角度////////////////////////////////////////////////////////////////////
        button_add_anger=findViewById(R.id.add_anger);
        button_reduce_anger=findViewById(R.id.reduce_anger);
        stop_in_anger=findViewById(R.id.stop_in_anger);
        PopupView_learning=getLayoutInflater().inflate(R.layout.learning,null);
        ////////////////learning界面变量/////////////////////////////////////////////////////////
        textView_already_learning_time=PopupView_learning.findViewById(R.id.already_learning_time);

        ////////////////////temperature/////////
        View PopupView_temperature=getLayoutInflater().inflate(R.layout.temperature,null);
        Button button_temperature_add=PopupView_temperature.findViewById(R.id.button_temperature_add);
        Button button_temperature_reduce=PopupView_temperature.findViewById(R.id.button_temperature_reduce);
        TextView textView_water_temperature=PopupView_temperature.findViewById(R.id.water_temperature);
        //////////////////////服务器变量
        out_desktop=findViewById(R.id.out_desktop);
        connect_pi=findViewById(R.id.connect_pi);
        connect_status=findViewById(R.id.connect_status);
        receive_status=findViewById(R.id.receive_status);
        port_in=findViewById(R.id.port);
        tx=new StringBuffer("x000000000f0000000000000000000c");
        rx=new StringBuffer("0000000000");
        ///////////////////////////////////连接按钮//////////////////////////////////////////////
        connect_pi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(status==0){
                    try{
                        port=Integer.parseInt(port_in.getText().toString());
                        connect_service.start();
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        out_desktop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(connect_ok==1){
                    tx.replace(9,10,"1");
                    send();
                }
                Process.killProcess(Process.myPid());
                System.exit(0);
            }
        });
        ///////////////////////连接服务器线程/////////////////////////////////////////////////////////

        connect_service=new Thread(new Runnable() {
            @Override
            public void run() {
                status=1;
                try {
                    s=new Socket(ip,port);
                    connect_status.setText("已连接");
                    connect_ok=1;
                } catch (IOException e) {
                    e.printStackTrace();
                }
                while (true){
                    receive();
                }
            }
        });
        //心跳包，防止断联
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    if(connect_ok==1){
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        tx.replace(9,10,"2");
                        send();
                    }
                }
            }
        }).start();
        //////////////时钟线程////////////////////////////////////////////////////////////////////////
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    cal=Calendar.getInstance();
                    cal.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
                    year = String.valueOf(cal.get(Calendar.YEAR));
                    month = String.valueOf(cal.get(Calendar.MONTH)+1);
                    day = String.valueOf(cal.get(Calendar.DATE));
                    week=String.valueOf(cal.get(Calendar.DAY_OF_WEEK)-1);
                    //Log.d("lxh",week);
                    if (cal.get(Calendar.AM_PM) == 0)
                        hour = String.valueOf(cal.get(Calendar.HOUR));
                    else
                        hour = String.valueOf(cal.get(Calendar.HOUR)+12);
                    minute = String.valueOf(cal.get(Calendar.MINUTE));
                    second = String.valueOf(cal.get(Calendar.SECOND));
                    if(Integer.parseInt(hour)<10){
                        hour="0"+hour;
                    }
                    if(Integer.parseInt(minute)<10){
                        minute="0"+minute;
                    }
                    if(Integer.parseInt(second)<10){
                        second="0"+second;
                    }
                    my_time_1 = year + "-" + month + "-" + day;
                    my_time_2 = hour + ":" + minute + ":" + second;
                    if(startRunTime==1){
                        already_learning_time_s=(Integer.parseInt(hour)-start_hour)*3600+(Integer.parseInt(minute)-start_minute)*60+(Integer.parseInt(second)-start_second);
                        already_learning_time_minute=already_learning_time_s/60;//时间回环
                        already_learning_time_hours=already_learning_time_minute/60;
                        already_learning_time_s=already_learning_time_s%60;
                        already_learning_time_minute=already_learning_time_minute%60;
                        already_learning_time_hours%=24;
                        int show_time_hour=already_learning_time_hours+learning_time_hour;
                        int show_time_minute=already_learning_time_minute+learning_time_minute;
                        int show_time_second=already_learning_time_s+learning_time_second;
                        show_time_minute+=show_time_second/60;
                        show_time_second%=60;
                        show_time_hour+=show_time_minute/60;
                        show_time_minute%=60;
                        String already_timeString=String.valueOf(show_time_hour)+"小时"+String.valueOf(show_time_minute)+"分钟"+String.valueOf(show_time_second)+"秒";
                        textView_already_learning_time.setText(already_timeString);
                    }
                    String my_time=my_time_1+','+my_time_2;
                    tx.replace(11,30,my_time);
                }
            }
        }).start();
        ///////////////////////调节桌面角度函数////////////////////////////////////////////////////////
        button_add_anger.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(flag_add_anger==0){
                    button_add_anger.setActivated(true);
                    flag_add_anger=1;
                    button_reduce_anger.setActivated(false);
                    flag_reduce_anger=0;
                }
                else if (flag_add_anger==1){
                    button_add_anger.setActivated(false);
                    flag_add_anger=0;
                }
                if(connect_ok==1){
                    tx.replace(3,5,"10");
                    send();
                }
            }
        });
        stop_in_anger.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(connect_ok==1){
                    tx.replace(3,5,"00");
                    send();
                }
            }
        });
        button_reduce_anger.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(flag_reduce_anger==0){
                    button_reduce_anger.setActivated(true);
                    flag_reduce_anger=1;
                    button_add_anger.setActivated(false);
                    flag_add_anger=0;
                }
                else if (flag_reduce_anger==1){
                    button_reduce_anger.setActivated(false);
                    flag_reduce_anger=0;
                }
                if(connect_ok==1){
                    tx.replace(3,5,"01");
                    send();
                }
            }
        });
        ///////////////////////调节高度按钮函数//////////////////////////////////////////////////////

        button_add_height.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(flag_add_high==0){
                    button_add_height.setActivated(true);
                    flag_add_high=1;
                    button_add_height.setActivated(false);
                    flag_add_high=0;
                }
                else if (flag_add_high==1){
                    button_add_height.setActivated(false);
                    flag_add_high=0;
                }
                if(connect_ok==1){
                    tx.replace(1,3,"10");
                    send();
                }
            }
        });
        stop_in_height.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(connect_ok==1){
                    tx.replace(1,3,"00");
                    send();
                }
            }
        });
        button_reduce_height.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(flag_reduce_high==0){
                    button_reduce_height.setActivated(true);
                    flag_reduce_high=1;
                    button_reduce_height.setActivated(false);
                    flag_reduce_high=0;
                }
                else if (flag_reduce_high==1){
                    button_reduce_height.setActivated(false);
                    flag_reduce_high=0;
                }
                if(connect_ok==1){
                    tx.replace(1,3,"01");
                    send();
                }
            }
        });
        //////////切换到分析界面//////
        button_analysis.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,MainActivity3.class);
                startActivity(intent);
                MainActivity.this.finish();
            }
        });
        button_self.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,self.class);
                startActivity(intent);
                MainActivity.this.finish();
            }
        });
        button_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,setting.class);
                startActivity(intent);
                MainActivity.this.finish();
            }
        });
        /////////////////////////界面切换/////////////////////////////////////////////////////////
        button_learning.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Button button_lock_screen=PopupView_learning.findViewById(R.id.button_lock_screen);
                Button button_learning_start=PopupView_learning.findViewById(R.id.button_learning_start);
                Button button_learning_stop=PopupView_learning.findViewById(R.id.button_learning_stop);
                Button button_back=PopupView_learning.findViewById(R.id.button_from_learning_to_desk);
                PopupWindow popupWindow=new PopupWindow(PopupView_learning, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT,true);
                popupWindow.showAsDropDown(v);
                button_learning_start.setOnClickListener(new View.OnClickListener() {
                    @SuppressLint("Range")
                    @Override
                    public void onClick(View v) {
                        if(startRunTime==0){
                            start_hour=Integer.parseInt(hour);
                            start_minute=Integer.parseInt(minute);
                            start_second=Integer.parseInt(second);
                            startRunTime=1;
                            SQLiteOpenHelper Helper=MySQliteOpenHelper.getInstance(MainActivity.this);
                            SQLiteDatabase db=Helper.getWritableDatabase();
                            if (db.isOpen()){
                                //先查询是否在数据库内
                                @SuppressLint("Recycle") Cursor cursor=db.rawQuery("select*from learning_data",null);
                                while (cursor.moveToNext()){
                                    @SuppressLint("Range") int _id=cursor.getInt(cursor.getColumnIndex("_id"));
                                    @SuppressLint("Range") String data_year=cursor.getString(cursor.getColumnIndex("data_year"));
                                    @SuppressLint("Range") String data_month=cursor.getString(cursor.getColumnIndex("data_month"));
                                    @SuppressLint("Range") String data_day=cursor.getString(cursor.getColumnIndex("data_day"));
                                    learning_time_hour =cursor.getInt(cursor.getColumnIndex("learning_time_hour"));
                                    learning_time_minute =cursor.getInt(cursor.getColumnIndex("learning_time_minute"));
                                    learning_time_second =cursor.getInt(cursor.getColumnIndex("learning_time_second"));
                                    if (data_year.equals(year)&&data_month.equals(month)&&data_day.equals(day)){//如果有数据
                                        break;
                                    }else{
                                        learning_time_hour=0;
                                        learning_time_minute=0;
                                        learning_time_second=0;
                                    }
                                }
                                cursor.close();
                                db.close();
                            }
                        }
                    }
                });
                button_learning_stop.setOnClickListener(new View.OnClickListener() {
                    @SuppressLint("Range")
                    @Override
                    public void onClick(View v) {
                        if(startRunTime==1){
                            startRunTime=0;
                            SQLiteOpenHelper Helper=MySQliteOpenHelper.getInstance(MainActivity.this);
                            SQLiteDatabase db=Helper.getWritableDatabase();
                            if (db.isOpen()){
                                //先查询是否在数据库内
                                @SuppressLint("Recycle") Cursor cursor=db.rawQuery("select*from learning_data",null);
                                while (cursor.moveToNext()){
                                    @SuppressLint("Range") int _id=cursor.getInt(cursor.getColumnIndex("_id"));
                                    @SuppressLint("Range") String data_year=cursor.getString(cursor.getColumnIndex("data_year"));
                                    @SuppressLint("Range") String data_month=cursor.getString(cursor.getColumnIndex("data_month"));
                                    @SuppressLint("Range") String data_day=cursor.getString(cursor.getColumnIndex("data_day"));
                                    learning_time_hour =cursor.getInt(cursor.getColumnIndex("learning_time_hour"));
                                    learning_time_minute =cursor.getInt(cursor.getColumnIndex("learning_time_minute"));
                                    learning_time_second =cursor.getInt(cursor.getColumnIndex("learning_time_second"));
                                    if (data_year.equals(year)&&data_month.equals(month)&&data_day.equals(day)){//如果是今天
                                        //直接将查询到的数据用于显示
                                        break;
                                    }else if (data_year.equals("year")&&data_month.equals("month")&&data_day.equals("day")){
                                        //如果没有今天的数据则置0，不用于显示
                                        learning_time_hour=0;
                                        learning_time_minute=0;
                                        learning_time_second=0;
                                        //每用掉一行得表就增加一行
                                        String sql = "insert into learning_data(data_year,data_month,data_day,learning_time_hour,learning_time_minute,learning_time_second) values('year','month','day','learning_hour','learning_minute','learning_second')";
                                        db.execSQL(sql);
                                        break;
                                    }else{
                                        save_id+=1;
                                    }
                                }
                                cursor.close();
                                //添加数据或更新数据,只有在点击停止时存数据
                                String sql;
                                sql = "update learning_data set data_year=? where _id=?";
                                db.execSQL(sql,new Object[]{year,save_id});
                                sql = "update learning_data set data_month=? where _id=?";
                                db.execSQL(sql,new Object[]{month,save_id});
                                sql = "update learning_data set data_day=? where _id=?";
                                db.execSQL(sql,new Object[]{day,save_id});

                                sql = "update learning_data set learning_time_hour=? where _id=?";
                                db.execSQL(sql,new Object[]{already_learning_time_hours+ learning_time_hour,save_id});
                                sql = "update learning_data set learning_time_minute=? where _id=?";
                                db.execSQL(sql,new Object[]{already_learning_time_minute+ learning_time_minute,save_id});
                                sql = "update learning_data set learning_time_second=? where _id=?";
                                db.execSQL(sql,new Object[]{already_learning_time_s+ learning_time_second,save_id});
                                db.close();
                            }
                        }
                    }
                });
                button_lock_screen.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LockScreen(v);
                    }
                });
                button_back.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });
            }
        });
        button_light.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("UseSwitchCompatOrMaterialCode")
            @Override
            public void onClick(View v) {
                View PopupView=getLayoutInflater().inflate(R.layout.light,null);
                Switch light_switch=PopupView.findViewById(R.id.switch_light);
                Switch light_auto=PopupView.findViewById(R.id.switch_light_auto);
                Button button_status1=PopupView.findViewById(R.id.button_light_status_1);
                Button button_status2=PopupView.findViewById(R.id.button_light_status_2);
                Button button_status3=PopupView.findViewById(R.id.button_light_status_3);
                Button button_status4=PopupView.findViewById(R.id.button_light_status_4);
                Button button_back=PopupView.findViewById(R.id.button_from_light_back_to_desk);
                PopupWindow popupWindow=new PopupWindow(PopupView, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT,true);
                popupWindow.showAsDropDown(v);
                //
                button_back.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });
                //////////////灯开关
                light_switch.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean isChecked = light_switch.isChecked();
                        if (isChecked){
                            if(connect_ok==1){
                                tx.replace(7,8,"1");
                                send();
                            }
                        }else{
                            if(connect_ok==1){
                                tx.replace(7,8,"0");
                                send();
                            }
                        }
                    }
                });
                ////////自动调节开关
                light_auto.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean is_light_open=light_switch.isChecked();
                        if (!is_light_open){
                            light_auto.setChecked(false);
                            Toast.makeText(MainActivity.this, "请先打开灯光再打开自动调节开关", Toast.LENGTH_SHORT).show();
                        }else {
                            ///按下自动体调节后，恢复四个亮度的按钮
                            button_status1.setActivated(false);
                            button_status2.setActivated(false);
                            button_status3.setActivated(false);
                            button_status4.setActivated(false);
                            flag_light_status=0;
                        }
                    }
                });
                ///////灯亮度开关
                button_status1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean is_light_open=light_switch.isChecked();
                        boolean is_light_auto_open=light_auto.isChecked();
                        if (!is_light_open){
                            light_auto.setChecked(false);
                            Toast.makeText(MainActivity.this, "请先打开灯光再打开自动调节开关", Toast.LENGTH_SHORT).show();
                        }else if(is_light_auto_open){
                            Toast.makeText(MainActivity.this, "已开启自动调节", Toast.LENGTH_SHORT).show();
                        }else{
                            if (flag_light_status==1){
                                button_status1.setActivated(false);
                                flag_light_status=0;
                            }
                            else{
                                button_status1.setActivated(true);
                                button_status2.setActivated(false);
                                button_status3.setActivated(false);
                                button_status4.setActivated(false);
                                flag_light_status=1;
                            }
                        }
                    }
                });
                button_status2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean is_light_open=light_switch.isChecked();
                        boolean is_light_auto_open=light_auto.isChecked();
                        if (!is_light_open){
                            light_auto.setChecked(false);
                            Toast.makeText(MainActivity.this, "请先打开灯光再打开自动调节开关", Toast.LENGTH_SHORT).show();
                        }else if(is_light_auto_open){
                            Toast.makeText(MainActivity.this, "已开启自动调节", Toast.LENGTH_SHORT).show();
                        }else {
                            if (flag_light_status == 2) {
                                button_status2.setActivated(false);
                                flag_light_status = 0;
                            } else {
                                button_status1.setActivated(false);
                                button_status2.setActivated(true);
                                button_status3.setActivated(false);
                                button_status4.setActivated(false);
                                flag_light_status = 2;
                            }
                        }
                    }
                });
                button_status3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean is_light_open=light_switch.isChecked();
                        boolean is_light_auto_open=light_auto.isChecked();
                        if (!is_light_open){
                            light_auto.setChecked(false);
                            Toast.makeText(MainActivity.this, "请先打开灯光再打开自动调节开关", Toast.LENGTH_SHORT).show();
                        }else if(is_light_auto_open){
                            Toast.makeText(MainActivity.this, "已开启自动调节", Toast.LENGTH_SHORT).show();
                        }else {
                            if (flag_light_status == 3) {
                                button_status3.setActivated(false);
                                flag_light_status = 0;
                            } else {
                                button_status1.setActivated(false);
                                button_status2.setActivated(false);
                                button_status3.setActivated(true);
                                button_status4.setActivated(false);
                                flag_light_status = 3;
                            }
                        }
                    }
                });
                button_status4.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean is_light_open=light_switch.isChecked();
                        boolean is_light_auto_open=light_auto.isChecked();
                        if (!is_light_open){
                            light_auto.setChecked(false);
                            Toast.makeText(MainActivity.this, "请先打开灯光再打开自动调节开关", Toast.LENGTH_SHORT).show();
                        }else if(is_light_auto_open){
                            Toast.makeText(MainActivity.this, "已开启自动调节", Toast.LENGTH_SHORT).show();
                        }else {
                            if (flag_light_status == 4) {
                                button_status4.setActivated(false);
                                flag_light_status = 0;
                            } else {
                                button_status1.setActivated(false);
                                button_status2.setActivated(false);
                                button_status3.setActivated(false);
                                button_status4.setActivated(true);
                                flag_light_status = 4;
                            }
                        }
                    }
                });
            }
        });
        button_temperature.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View v) {
                PopupWindow popupWindow=new PopupWindow(PopupView_temperature, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT,true);
                popupWindow.showAsDropDown(v);
                Button button_change_to_45=PopupView_temperature.findViewById(R.id.button_change_to_45);
                Button button_change_to_60=PopupView_temperature.findViewById(R.id.button_change_to_60);
                Button button_change_to_80=PopupView_temperature.findViewById(R.id.button_change_to_80);
                Button button_back=PopupView_temperature.findViewById(R.id.button_from_temperature_back_to_desk);
                button_back.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });
                button_temperature_add.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        StringBuffer send_temperature;
                        send_temperature=new StringBuffer("00");
                        String water_temperature=textView_water_temperature.getText().toString();
                        int_water_temperature=Integer.parseInt(water_temperature);
                        int_water_temperature+=1;
                        if(int_water_temperature<10&&int_water_temperature>=0){
                            send_temperature.replace(1,2,String.valueOf(int_water_temperature));
                        }else if(int_water_temperature>=10&&int_water_temperature<100){
                            send_temperature.replace(0,2,String.valueOf(int_water_temperature));
                        }else if(int_water_temperature<0){
                            send_temperature.replace(0,2,"00");
                        }else {
                            send_temperature.replace(0,2,"99");
                        }
                        textView_water_temperature.setText(String.valueOf(int_water_temperature));
                        if(connect_ok==1){
                            tx.replace(5,7, String.valueOf(send_temperature));
                            send();
                        }
                    }
                });
                button_temperature_reduce.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        StringBuffer send_temperature;
                        send_temperature=new StringBuffer("00");
                        String water_temperature=textView_water_temperature.getText().toString();
                        int_water_temperature=Integer.parseInt(water_temperature);
                        int_water_temperature-=1;
                        if(int_water_temperature<10&&int_water_temperature>=0){
                            send_temperature.replace(1,2,String.valueOf(int_water_temperature));
                        }else if(int_water_temperature>=10&&int_water_temperature<100){
                            send_temperature.replace(0,2,String.valueOf(int_water_temperature));
                        }else if(int_water_temperature<0){
                            send_temperature.replace(0,2,"00");
                        }else {
                            send_temperature.replace(0,2,"99");
                        }
                        textView_water_temperature.setText(String.valueOf(int_water_temperature));
                        if(connect_ok==1){
                            tx.replace(5,7, String.valueOf(send_temperature));
                            send();
                        }
                    }
                });
                button_change_to_45.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        textView_water_temperature.setText("45");
                        if(connect_ok==1){
                            tx.replace(5,7, "45");
                            send();
                        }
                    }
                });
                button_change_to_60.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        textView_water_temperature.setText("60");
                        if(connect_ok==1){
                            tx.replace(5,7, "60");
                            send();
                        }
                    }
                });
                button_change_to_80.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        textView_water_temperature.setText("80");
                        if(connect_ok==1){
                            tx.replace(5,7, "80");
                            send();
                        }
                    }
                });
            }
        });
        button_heat_radiation.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("UseSwitchCompatOrMaterialCode")
            @Override
            public void onClick(View v) {
                View PopupView_heat_radiation=getLayoutInflater().inflate(R.layout.heat_radiation,null);
                Switch switch_heat_radiation=PopupView_heat_radiation.findViewById(R.id.switch_heat_radiation);
                Button button_back=PopupView_heat_radiation.findViewById(R.id.button_from_radiation_to_desk);
                PopupWindow popupWindow=new PopupWindow(PopupView_heat_radiation, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT,true);
                popupWindow.showAsDropDown(v);
                switch_heat_radiation.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean isChecked = switch_heat_radiation.isChecked();
                        if (isChecked){
                            if(connect_ok==1){
                                tx.replace(8,9,"1");
                                send();
                            }
                        }else{
                            if(connect_ok==1){
                                tx.replace(8,9,"0");
                                send();
                            }
                        }
                    }
                });
                button_back.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });
            }
        });
    }
    @SuppressLint("SetTextI18n")
    private void receive(){
        try {
            InputStream inputStream = s.getInputStream();
            InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
            char []cha = new char[1024];
            int len = inputStreamReader.read(cha);
            receive_status.setText(new String(cha,0,len));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }//应该改为相应式发送
    private void send(){
        OutputStream outputStream=null;
        try {
            String send_buff=String.valueOf(tx);
            outputStream=s.getOutputStream();
            outputStream.write(send_buff.getBytes());
            connect_status.setText(send_buff);
            outputStream.flush();
            tx.replace(1,10,"000000000");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void LockScreen(View v){
        policyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        componentName = new ComponentName(this, LockScreenAdmin.class);
        if (policyManager.isAdminActive(componentName)) {//判断是否有权限(激活了设备管理器)
            policyManager.lockNow();// 直接锁屏
            //android.os.Process.killProcess(android.os.Process.myPid());
        }else{
            activeManager();//激活设备管理器获取权限
        }
    }

    public void Bind(View v){
        if(componentName!=null){
            policyManager.removeActiveAdmin(componentName);
            activeManager();
        }
    }
    @Override
    protected void onResume() {//重写此方法用来在第一次激活设备管理器之后锁定屏幕
        if (policyManager!=null && policyManager.isAdminActive(componentName)) {
            policyManager.lockNow();
            android.os.Process.killProcess(android.os.Process.myPid());
        }
        super.onResume();
    }
    private void activeManager() {
        //使用隐式意图调用系统方法来激活指定的设备管理器
        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "一键锁屏");
        startActivity(intent);
    }
    private void flag_init(){
        int flag_add_high=0;
        int flag_reduce_high=0;
    }

}